package com.cupk.mapper;

import com.cupk.pojo.Stage;
import com.cupk.pojo.Topic;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface StageMapper {
    @Select("select max(period_id) from stage where match_id = #{matchId}")
    Integer findMaxPeriod(Integer matchId);

    @Insert("insert into stage(match_id,period_id,name,rule,standard,attach,start_time,end_time)" + " values (#{matchId}, #{periodId}, #{name}, #{rule}, #{standard}, #{attach}, #{startTime}, #{endTime})")
    void addStage(Stage stage);

    @Select("select * from stage where match_id = #{matchId} order by period_id asc")
    List<Stage> getStageListByMatch(Integer matchId);

    @Select("select * from stage where id = #{stageId}")
    Stage getStageById(Integer stageId);

    @Update("update stage set name = #{name}, rule = #{rule}, standard = #{standard}, attach = #{attach}, start_time = #{startTime}, end_time = #{endTime} where id = #{id}")
    void updateStage(Stage stage);

    @Delete("delete from stage where id = #{stageId}")
    void deleteStage(Integer stageId);

    @Select("select * from stage where match_id = #{matchId} and start_time <= #{nowTime} and end_time >= #{nowTime} and id in (select stage_id from topic where id not in (select topic_id from record where stu_id = #{stuId} and status = 1))order by period_id asc")
    List<Stage> getEffectListByMatchAndStudent(Integer matchId, Integer stuId, LocalDateTime nowTime);

    @Select("select * from stage where id in (select stage_id from topic where id = #{topicId})")
    Stage getRuleByTopic(Integer topicId);

    @Select("select count(DISTINCT stu_id) from record where topic_id in (select id from topic where stage_id = #{stageId})")
    Integer getAcceptPeople(Integer stageId);

    @Select("select count(DISTINCT stu_id) from record where topic_id in (select id from topic where stage_id = #{stageId})")
    Integer getUnAcceptPeople(Integer stageId);

    @Select("select max(id) from stage where match_id = #{matchId}")
    Integer findMaxStage(Integer matchId);

    @Select("select max(id) from stage where match_id = #{matchId}")
    Integer findMinStage(Integer matchId);
}
